//
//  LWHomeTabCell.swift
//  Swift_Weibo_HLW
//
//  Created by huanglinwang on 16/9/23.
//  Copyright © 2016年 Linwang. All rights reserved.
//

import UIKit
//首页自定义Cell

// 原创微博视图LWStatusOrginalView

// 转发微博视图
// 底部toolBar视图

//子控件的内容间距
let LWHomeTabCellMargin: CGFloat = 10


class LWHomeTabCell: UITableViewCell {

    
    //  记录当前toolbar顶部约束
    var toolBarTopConstraint: Constraint?
    
    
    // cell 需要使用ViewModel
    var statusViewModel:LWStatusViewModel?{
        didSet{
            
            //拿到数据给Cell里面对应的控件绑定数据
            originalView.statusViewModel = statusViewModel
            toolBar.statusViewModel = statusViewModel
            
            
            //  把toolbar顶部约束卸载掉
            toolBarTopConstraint?.uninstall()

            //  判断其是否有转发微博
            if statusViewModel?.status?.retweeted_status != nil {
                //  有转发微博
                //  更新约束,显示转发微博,绑定数据
                retweetView.isHidden = false
                toolBar.snp_updateConstraints(closure: { (make) in
                    toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })

                retweetView.statusViewModel = statusViewModel
            } else {
                //  没有转发微博
                //  更新约束, 隐藏转发微博视图
                retweetView.isHidden = true
                toolBar.snp_updateConstraints(closure: { (make) in
                    toolBarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
                
                
                
            }

            
            
        }
    }
    
    // MARK: -  懒加载控件
    private lazy var originalView : LWStatusOrginalView = {
       
        let view = LWStatusOrginalView()
//        view.backgroundColor = RandomColor()
//        view.backgroundColor = UIColor(colorLiteralRed: Float(217) / 255, green: Float(217)/255, blue: Float(217)/255, alpha: 1)
        view.backgroundColor = UIColor.white
        return view
    }()
    
    // toolBar视图
    private lazy var toolBar: LWStatusToolBar = LWStatusToolBar()
    
    // 转发微博视图
    private lazy var retweetView : LWStatusRetweetView = {
        let view  = LWStatusRetweetView()
        
        view.backgroundColor = UIColor(colorLiteralRed: CFloat(245) / 255, green: CFloat(245)/255, blue: CFloat(245)/255, alpha: 1)
       
        return view
    }()
    
    
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
    }
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //搭建界面
    private func setupUI(){
        //添加控制键设置约束
        contentView.addSubview(originalView)
        contentView.addSubview(toolBar)
        contentView.addSubview(retweetView)
        
        
        originalView.snp_makeConstraints { (make) in
            make.top.equalTo(contentView)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
        retweetView.snp_makeConstraints { (make) in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.equalTo(originalView)
            make.trailing.equalTo(originalView)
            make.bottom.equalTo(toolBar.snp_top)
        }
        
        toolBar.snp_makeConstraints { (make) in
            toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.equalTo(originalView)
            make.trailing.equalTo(originalView)
            make.bottom.equalTo(contentView)
            make.height.equalTo(35)
        }


    }

}
